<template>
  <div class="home">
    <nav>
      <van-tabs
        class="tabs"
        @click="onChannelChange"
      >
        <van-tab
          v-for="item in channelList"
          :key="item.id"
          :title="item.name"
          :name="item.id"
        />
      </van-tabs>
      <div class="icon-wrapper">
        <van-icon
          name="search"
          size="20"
          @click=" onSearch"
        />
      </div>
      <div class="icon-wrapper">
        <van-icon
          name="ellipsis"
          size="20"
        />
      </div>
    </nav>
    <main
      class="main"
    >
      <van-loading
        color="#1989fa"
        class="loading"
        v-if="articleListLoading"
      />
      <van-empty
        v-if="!articleList"
        image="error"
        description="请求错误"
      />
      <van-empty
        v-else-if="!articleList.length"
        description="暂无数据"
      />
      <article-list
        v-else
        :data="articleList"
        @click="goArticleDetails"
      />
    </main>
  </div>
</template>

<script>
import axios from 'axios';
import {
  mapActions,
  mapState,
} from 'vuex';
import moment from 'moment'
export default {
  name: 'HomePage',
  data() {
    return {
      articleListLoading: true,
    }
  },
  computed: {
    // 注入数据
    ...mapState('channel', [
      'channelList',
    ]),
    ...mapState('article', [
      'articleList',
    ]),
  },
  methods: {
    moment() {
      return moment;
    },
    getTime(timestamp) {
      return moment(timestamp).fromNow();
    },
    onSearch() {
      this.$router.push('/search');
    },
    goArticleDetails() {
      this.$router.push('/article')
    },
    // 频道
    ...mapActions('channel', [
      'updateChannelList',
    ]),
    // 文章
    ...mapActions('article', [
      'updateArticleList'
    ]),
    async updateArticleListWithLoading(id) {
      this.articleListLoading = true;
      await this.updateArticleList(id);
      this.articleListLoading = false;
    },
    onChannelChange(id) {
      this.updateArticleListWithLoading(id);
    },
  },
  // 初始化数据  发请求
  async created() {
    this.updateChannelList();
    this.updateArticleListWithLoading(0)
  },
}
</script>

<style lang="less" scoped>
@import url('@/assets/styles/common.less');

.home {
  .layout;

  nav {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-around;
    box-shadow: 0 3px 3px #e9e9e9;

    .tabs {
      flex: 1;
      overflow: hidden;
      position: relative;

      &::after {
        content: '';
        display: block;
        height: 100%;
        width: 40px;
        background: linear-gradient(to right, transparent, white);
        position: absolute;
        top: 0;
        right: 0;
      }
    }

    .icon-wrapper {
      margin: 8px;

      &:last-child {
        margin-right: 16px;
      }
    }
  }

  main {
    padding: 0 8px;
    overflow: auto;
    height: calc(100vh - 94px);
    .article {
      padding: 8px 0;
      border-bottom: 1px solid #e9e9e9;

      &:last-child {
        border: none;
      }

      .article-type1 section {
        grid-template-columns: 2fr 1fr;
        gap: 16px;
      }

      .article-other section {
        grid-template-columns: 1fr 1fr 1fr;
        gap: 8px;
      }

      section {
        display: grid;
        margin: 8px 0;

        .img-wrapper {
          height: 82px;
          overflow: hidden;
          border-radius: 4px;

          img {
            height: 100%;
            width: 100%;
            // 图片适应父盒子的宽高
            object-fit: cover;
          }
        }
      }

      footer {
        .sub-text;

        span {
          margin-right: 8px;
        }
      }
    }
    .loading{
      margin: auto;
      margin-left: 50%;
      display: inline-block;
    }
  }
}

</style>
